# coding=utf-8

from settings import USER_AGENTS
from settings import PROXIES
import random
import base64

class RandomUserAgent(object):

    def process_request(self, request, spider):
        useragent = random.choice(USER_AGENTS)
        request.headers.setdefault("User-Agent",useragent)



class RandomProxy(object):

    def process_request(self, request, spider):
        proxy = random.choice(PROXIES)

        if proxy["user_passwd"] is None:
            # 没有代理帐号验证的代理使用方式
            request.meta["proxy"] = "http://" + proxy["ip_port"]
        else:
            # 对帐号密码进行base64转行
            base64_userpasswd = base64.b64decode(proxy["user_passwd"])
            # 对应道代理服务器的信令格式里
            request.headers["Proxy-Authorization"] = "Basic " + base64_userpasswd
            request.meta["proxy"] = "http://" + proxy["ip_port"]
